//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    this.setkecheng();
    
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {

        if (res.authSetting['scope.userInfo']) {
          this.getUserinfo();
        }
      }
    });
  },
  //type--1为选择       type--7为5选单选
  //type--2为阅读       type--8为多选
  //type--3为文本题     type--9为B型选择
  //type--4为图片题     type--10为数学解答题
  //type--5为填空题     55
  //type--6辨析题
  setkecheng:function(){
    let kecheng = {
      "gqbyw": [{
        "id": 1,
        "name": "基础知识题",
        "type":1
      },
        {
          "id": 2,
          "name": "现代文阅读1",
          "type":2
        },
        {
          "id": 3,
          "name": "现代文阅读2",
          "type":3
        },
        {
          "id": 44,
          "name": "文言文",
          "type": 3
        },
        {
          "id": 56,
          "name": "古诗文",
          "type": 3
        },
        {
          "id": 45,
          "name": "作文",
          "type": 3
        }],
      "gqbsx1":[{
        "id":4,
        "name":"选择题",
        "type":4
      },
      {
        "id":5,
        "name":"填空题",
        "type":5
      },
      {
        "id":6,
        "name":"解答题",
        "type":10
      }],
      "gqbsx2": [{
        "id": 7,
        "name": "选择题",
        "type": 4
      },
      {
        "id": 8,
        "name": "填空题",
        "type": 5
      },
      {
        "id": 9,
        "name": "解答题",
        "type": 10
      }],
      "gqbyy":[{
        "id":10,
        "name":"语音知识",
        "type":1
      },
      {
        "id": 46,
        "name": "词汇与语法知识",
        "type": 1
      },
      {
        "id":11,
        "name":"阅读理解",
        "type":2
      },
      {
        "id": 50,
        "name": "补全对话",
        "type": 10
      },
      {
        "id": 47,
        "name": "完形填空",
        "type": 10
      },
      {
        "id":12,
        "name":"作文",
        "type":3
      }],
      "gqblh": [{
        "id": 13,
        "name": "选择题",
        "type": 4
      }],
      "gqbsd": [{
        "id": 53,
        "name": "选择题",
        "type": 1
      }],
      "gqzsx1": [{
        "id": 14,
        "name": "选择题",
        "type": 4
      },
      {
        "id":15,
        "name":"填空题",
        "type":5
      }, 
      {
        "id": 16,
        "name": "解答题",
        "type": 10
      }],
      "gqzsx2": [{
        "id": 17,
        "name": "选择题",
        "type": 4
      },
      {
        "id": 18,
        "name": "填空题",
        "type": 5
      },
      {
        "id": 19,
        "name": "解答题",
        "type": 10
      }],
      "gqzyy": [{
        "id": 20,
        "name": "语音知识",
        "type": 1
      },
      {
        "id": 26,
        "name": "词汇与语法知识",
        "type": 1
      },
      {
        "id": 21,
        "name": "阅读理解",
        "type": 2
      },
        {
          "id": 51,
          "name": "补全对话",
          "type": 10
        }, 
      {
        "id": 48,
        "name": "完形填空",
        "type": 10
      },
      {
        "id": 22,
        "name": "作文",
        "type": 3
      }],
      "gqzyw": [{
        "id": 23,
        "name": "基础知识题",
        "type": 1
      },
      {
        "id": 24,
        "name": "现代文阅读1",
        "type": 2
      },
      {
        "id":25,
        "name": "现代文阅读2",
        "type": 3
      },
      {
        "id": 54,
        "name": "文言文",
        "type": 3
      },
      {
        "id": 57,
        "name": "古诗文",
        "type": 3
      },
      {
        "id": 55,
        "name": "作文",
        "type": 3
      }
      ],
      "zsbzz": [{
        "id": 27,
        "name": "选择题",
        "type": 1
      },
      {
        "id": 28,
        "name": "辨析题",
        "type": 6
      },
      {
        "id": 29,
        "name": "简答题",
        "type": 6
      },
      {
        "id": 30,
        "name": "论述题",
        "type": 6
      }],
      "zsbsx1": [{
        "id": 31,
        "name": "选择题",
        "type": 4
      },
      {
        "id": 32,
        "name": "填空题",
        "type": 5
      },
      {
        "id": 33,
        "name": "解答题",
        "type": 10
      }],
      "zsbsx2": [{
        "id": 34,
        "name": "选择题",
        "type": 4
      },
      {
        "id": 35,
        "name": "填空题",
        "type": 5
      },
      {
        "id": 36,
        "name": "解答题",
        "type": 10
      }],
      "zsbyy": [{
        "id": 37,
        "name": "语音",
        "type": 1
      },
      {
        "id": 38,
        "name": "选择题",
        "type": 1
      },
      {
        "id": 39,
        "name": "阅读理解",
        "type": 2
      },
        {
          "id": 52,
          "name": "补全对话",
          "type": 10
        },
        {
          "id": 49,
          "name": "完形填空",
          "type": 10
        },
       {
        "id": 40,
        "name": "作文",
        "type": 3
      }],
      "zsbyxzh": [{
        "id": 41,
        "name": "A型选择题",
        "type": 7
      },
      {
        "id": 42,
        "name": "B型选择题",
        "type": 9
      },
      {
        "id": 43,
        "name": "X型多选题",
        "type": 8
      }],
    }

    wx.setStorageSync("subject", kecheng);
  },

  //登陆过的用户登录
  getUserinfo: function () {
    var _this = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.login({
      success: function (res) {
        if (res.code) {
          wx.getUserInfo({
            withCredentials: true,
            success: function (res_user) {
              wx.setStorageSync("user", res_user.userInfo)
              console.log(res_user);
              wx.request({
                url: _this.globalData.apiurl + 'csessioninfo/login',
                method: 'POST',
                data: {
                  code: res.code,
                  encryptedData: res_user.encryptedData,
                  iv: res_user.iv,
                  rawData:res_user.rawData,
                  signature:res_user.signature
                },
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                success: function (datas) {
                  console.log(_this.globalData.userInfo)
                  if (_this.userInfoReadyCallback) {
                    _this.userInfoReadyCallback(res)
                  }
                  wx.hideLoading();
                }
              });

            }
          })
        }
      }
    })
  },

  //新用户登录
  setUserinfo: function () {
    var _this = this;
    //先判断缓存中有没有 


    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.login({
      success: function (res) {
        if (res.code) {
          wx.getUserInfo({
            withCredentials: true,
            success: function (res_user) {
              wx.setStorageSync("user", res_user.userInfo)
              console.log('res_user：',res_user)
              wx.request({
                url: _this.globalData.apiurl + 'csessioninfo/login',
                method: 'POST',
                data: {
                  code: res.code,
                  encryptedData: res_user.encryptedData,
                  iv: res_user.iv,
                  rawData: res_user.rawData,
                  signature: res_user.signature
                },
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                success: function (datas) {
                  console.log(datas.data)
                  _this.globalData.userInfo = datas.data;
                  
                  if (_this.userInfoReadyCallback) {
                    _this.userInfoReadyCallback(res)
                  }
                  wx.hideLoading();
                }
              });

            }
          })
        }
      }
    })
  },


  globalData: {
    userInfo: null,
    apiurl: "https://xlr.houhh.work/api/",
  },
})